﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace PushBox
{
    /// <summary>
    /// 游戏地图集合
    /// </summary>
    class Maps
    {
        //0空白区域，1目标位置，2箱子，3归位的箱子，4搬运工，5搬运工踩在目标位置上，8墙
        static Maps()
        {
            string path = "map-all.txt";
            var lines = File.ReadAllLines(path).Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => string.Join(";", x.Split(';').Take(3)));

            if(File.Exists("map-solvers.txt"))
            {
                var oks = File.ReadAllLines("map-solvers.txt").Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => string.Join(";", x.Split(';').Take(3)));

                lines = lines.Except(oks);
            }
            if(File.Exists("map-nosolvers.txt"))
            {
                var nos = File.ReadAllLines("map-nosolvers.txt").Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => string.Join(";", x.Split(';').Take(3)));

                lines = lines.Except(nos);
            }
            if(File.Exists("map-hards.txt"))
            {
                var hds = File.ReadAllLines("map-hards.txt").Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => string.Join(";", x.Split(';').Take(3)));

                lines = lines.Except(hds);

                if(!lines.Any())
                {
                    lines = hds;

                    if(File.Exists("map-solvers.txt"))
                    {
                        var oks = File.ReadAllLines("map-solvers.txt").Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => string.Join(";", x.Split(';').Take(3)));

                        lines = lines.Except(oks);
                    }
                    if(File.Exists("map-nosolvers.txt"))
                    {
                        var nos = File.ReadAllLines("map-nosolvers.txt").Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => string.Join(";", x.Split(';').Take(3)));

                        lines = lines.Except(nos);
                    }
                    RunHard = true;
                }
            }

            //var solvers = "mapwithlessstep.txt";
            //var sols = File.ReadAllLines(solvers).Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => string.Join(";", x.Split(';').Take(3)));

            //lines = lines.Except(sols).OrderBy(x => x.Length).ThenBy(x => x.Count(i => i == '2' || i == '3')).Distinct();

            //string path2 = "PushBox.map";
            //var lines2 = File.ReadAllLines(path2).Where(x => !string.IsNullOrWhiteSpace(x));
            //map.AddRange(lines2);

            //map = map.Distinct().OrderBy(x => x.Length).ThenBy(x => x.Count(i => i == '2' || i == '3')).ToList();
            lines = lines.OrderBy(x => x.Length).ThenBy(x => x.Count(i => i == '2' || i == '3'));
            AllMap = lines.ToList();
            if(!RunHard)
            {
                File.WriteAllLines(path, lines);
            }
            //using (var fs = File.OpenRead(path))
            //{
            //    using (var fr = new StreamReader(fs)) 
            //    {
            //        string line;
            //        while (!string.IsNullOrWhiteSpace(line = fr.ReadLine()))
            //        {
            //            map.Add(line);
            //        }
            //        fr.Close();
            //    }
            //    fs.Close();
            //}
        }

        /// <summary>
        /// 返回所有地图
        /// </summary>
        public static List<string> AllMap { get; private set; }

        public static bool RunHard = false;
    }
}
